﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECRMS.Biz.Dom;
using ECRMS.Data;

namespace ECRMS.Biz
{
    public class EcCorpLogic
    {
        public List<EcTrade> GetTrades()
        {
            using (var dataContext = ECRMSDataContext.NewInstance())
            {
                return (from t in dataContext.GetTable<ECTrade>()
                        select new EcTrade
                        {
                            TradeId = t.TradeId,
                            Name = t.Name
                        }).ToList();
            }
        }

        public bool SaveTrade(EcTrade trade)
        {
            using (var dataContext = ECRMSDataContext.NewInstance())
            {
                ECTrade dbTrade = null;
                if (trade.TradeId != 0)
                {
                    dbTrade = dataContext.GetTable<ECTrade>()
                        .FirstOrDefault(q => q.TradeId == trade.TradeId);
                }

                if (dbTrade == null)
                {
                    dbTrade = new ECTrade();
                    dataContext.GetTable<ECTrade>().InsertOnSubmit(dbTrade);
                }

                dbTrade.Name = trade.Name;

                dataContext.SubmitChanges();

                return true;
            }
        }

        public List<EcCorp> GetCorps()
        {
            using (var dataContext = ECRMSDataContext.NewInstance())
            {
                return (from corp in dataContext.GetTable<ECCorporation>()
                        select new EcCorp
                        {
                            CorporationRefId = corp.CorporationRefId,
                            Name = corp.Name,
                            City = corp.City,
                            Address = corp.Address,
                            Phone = corp.Phone,
                            Zipcode = corp.Zipcode,
                            TradeId = corp.TradeId,
                            CreateDate = corp.CreateDate
                        }).ToList();
            }
        }

        public bool SaveCorp(EcCorp corp)
        {
            using (var dataContext = ECRMSDataContext.NewInstance())
            {
                ECCorporation dbCorp = null;
                if (corp.CorporationRefId != Guid.Empty)
                    dbCorp = dataContext.GetTable<ECCorporation>()
                        .FirstOrDefault(q => q.CorporationRefId == corp.CorporationRefId);
                else
                    corp.CorporationRefId = Guid.NewGuid();

                if (dbCorp == null)
                {
                    dbCorp = new ECCorporation
                    {
                        CorporationRefId = corp.CorporationRefId,
                        CreateDate = DateTime.Now,
                        IsDisabled = false
                    };

                    dataContext.GetTable<ECCorporation>().InsertOnSubmit(dbCorp);
                }

                dbCorp.Name = corp.Name;
                dbCorp.City = corp.City;
                dbCorp.Address = corp.Address;
                dbCorp.Phone = corp.Phone;
                dbCorp.Zipcode = corp.Zipcode;
                dbCorp.TradeId = corp.TradeId;

                dataContext.SubmitChanges();

                return true;
            }
        }
    }
}
